/*
 * ADC.h
 * 
 */
#ifndef ADC_H
#define ADC_H 

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>

/*
#define PIN_SPI_CE0 10
#define PIN_SPI_CLK 14
#define PIN_SPI_MOSI 12
#define PIN_SPI_MISO 13
*/

int spi_fd;

void ADC_Setup(void)
{
	spi_fd = wiringPiSPISetup(0,15*100000);
	if( spi_fd == -1 ) 
	{printf("spi初始化失败!\n"); return;} 
	else 
	{printf("spi初始化成功!\n");}
}

void ADC_start(void)
{
	unsigned char buff[3] = {0x01,0x80,0x00};
	wiringPiSPIDataRW (0, buff, 3*sizeof(char)) ;
	
	int a = 0x03 & buff[1];
	a = ( (a << 8 ) + buff[2] );
	//printf("测得电压: %d V\n",a); //0~1024
	float b = (float)a  / 1024 *3.3 ; //Vin = ADC * Vref / 1024
	 printf("测得电压: %.2f V\n",b);
}

#endif
